package org.example.configcenter;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;
import java.util.Properties;

public class ConfigCenterClient {
    private final ConfigCenterProperties properties;

    public ConfigCenterClient(ConfigCenterProperties properties) {
        this.properties = properties;
    }

    public Map<String, Object> fetchConfigs() {
        // 实现从配置中心拉取配置的逻辑
        // 这里以Nacos为例
        try {
            Properties nacosProperties = new Properties();
            nacosProperties.put("serverAddr", properties.getServerAddr());
            nacosProperties.put("namespace", properties.getNamespace());

//            ConfigService configService = NacosFactory.createConfigService(nacosProperties);
//            String content = configService.getConfig("your-data-id", properties.getGroup(), 5000);
            String content = "";

            // 假设返回的是JSON格式的配置
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(content, new TypeReference<Map<String, Object>>() {});
        } catch (Exception e) {
            throw new RuntimeException("Failed to fetch config from config center", e);
        }
    }
}
